home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
CC_C
/
0924.ZIP
/
SCANF
< prev
next >
Wrap
Text File
|
1988-01-10
|
3KB
|
95 lines
/* SCANF/FSCANF/SSCANF FUNCTIONS - 16 BIT INPUT ONLY */
scanf (fs) int *fs;
{ int getc(), ungetc(); return _ffin (stdin, fs, getc, ungetc); }
fscanf (fs) int *fs;
{ int getc(), ungetc(); return _ffin(*fs, --fs, getc, ungetc); }
sscanf (fs) int *fs;
{ int _mgt(), _mungt(); return _ffin(fs, --fs, _mgt, _mungt); }
_ffin (fp, pp, read, ungetc)
char *fp, **pp; int (*read)(), (*ungetc)();
{ static int bb, base, value, asflag, sflag,
ch, temp, i, count, width;
static char *ff, c; int _ffgd();
ff = *pp--; count = 0;
while (c = *ff++)
{ if (isspace (c)) continue;
if (c != '%')
{ while (isspace (ch = (*read) (fp)));
if (ch == c) continue;
else return (ch == EOF) ? EOF : count; }
sflag = asflag = 0;
if ((*ff) == '*') { ++asflag; ++ff; }
width = isdigit (*ff) ? 0 : -1;
if (width == 0)
{ while ((c = *ff) >= '0' && c <= '9')
{ width = width * 10 + (c - '0'); ++ff; } }
switch ( toupper (c = *ff++))
{ case 'D':
case 'U': base = 10; goto decode;
case 'O': base = 8; goto decode;
case 'X': base = 16;
decode:
bb = 0; while (isspace (ch = (*read) (fp)));
if (ch == EOF) return EOF;
if (width && (ch == '+' || ch == '-'))
{ if (ch == '-') ++sflag;
ch = (*read) (fp); --width; }
if (width && base == 16 && ch == '0')
{ temp = (*read) (fp); --width;
if (toupper (temp) == 'X' && width)
{ ch = (*read) (fp); --width; }
else
{ (*ungetc) (temp, fp); ++width; } }
if ((value = _ffgd(ch, base)) == -1) return count;
while (width && (value != -1))
{ bb = bb * base + value;
ch = (*read) (fp); --width;
value = _ffgd (ch, base); }
(*ungetc) (ch, fp);
if (sflag) bb = - bb;
if (!asflag) { * (int *) (*pp) = bb; ++count; --pp; }
break;
case 'S':
while (isspace (ch = (*read) (fp)));
if (ch == EOF) return EOF;
while (width && ch != ' ' && ch != '\t' && ch != '\r'
&& ch != '\n' && ch != EOF)
{ if (!asflag) *(*pp)++ = ch;
ch = (*read) (fp); --width; }
(*ungetc) (ch, fp);
if (!asflag)
{ *(*pp) = '\0'; ++count; --pp; }
break;
case 'C':
if ((ch = (*read) (fp)) == EOF) return EOF;
else if (!asflag) { *(*pp) = ch; ++count; --pp; }
break;
default: return count;
} }
return count;
} /* End of _ffin */
/* Utilities for scanf and sscanf */
_ffgd (ch, base)
{ if (ch >= '0' && ch <= '9') ch -= '0';
else if (isalpha (ch = toupper (ch))) ch -= ('A' - 10);
else return -1;
return (ch < base) ? ch : -1; }
_mgt (s) char **s; { return (*(*s) != '\0') ? *(*s)++ : EOF; }
_mungt (c, s) int c; char **s; { --(*s); return c; }